/*
 * Copyright© 2013-2018 YZTC 
 * Author zhenghl 
 * 本公司保留所有下述内容的权利; 
 * 本内容为保密信息，仅限本公司内部使用; 
 * 非经本公司书面许可，任何人不得外泄或用于其他目的; 
*/
package com.wxmp.core.interceptor;
import java.io.IOException; 
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.jasig.cas.client.util.AssertionHolder;
import org.jasig.cas.client.validation.Assertion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.wxmp.core.util.SessionUtil;
import com.wxmp.wxapi.process.WxMemoryCacheClient;
import com.wxmp.wxcms.domain.Account;
import com.wxmp.wxcms.domain.SysUser;
import com.wxmp.wxcms.service.AccountService;
import com.wxmp.wxcms.service.SysUserService;
/**
 * 该过滤器用户从CAS认证服务器中获取登录用户用户名，并填充必要的Session.
 * @author luck
 *
 */
public class CasForInvokeContextFilter implements Filter {
	protected final static  Logger logger = LoggerFactory.getLogger(CasForInvokeContextFilter.class);
	private String casServerLoginUrl; 
	@Override
	public void destroy() {
	}
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpSession session = ((HttpServletRequest) request).getSession();
		if (SessionUtil.getUser()==null) {
			Assertion assertion = AssertionHolder.getAssertion();
			String userName = assertion.getPrincipal().getName();
			try {
				ServletContext context =  session.getServletContext();
                ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
                SysUserService sysUserService = ctx.getBean(SysUserService.class);
                AccountService accountService = ctx.getBean(AccountService.class);
				SysUser sysUser = sysUserService.getSysUserByAccount(userName);
				if(sysUser!=null) {
	                SessionUtil.setUser(sysUser);
	                Account account = accountService.getSingleAccount();
	                if (account != null) {
	                    WxMemoryCacheClient.setAccount(account.getAccount());
	                }
					logger.info("成功匹配登录用户："+userName); 
					chain.doFilter(request, response);
				}else {
					logger.info("匹配登录用户失败，重定向到登录页面");
					((HttpServletResponse) response).sendRedirect(casServerLoginUrl);
				}
			} catch (Exception e) {
				logger.error("cas获取用户登录信息过滤器异常",e); 
				((HttpServletResponse) response).sendRedirect(casServerLoginUrl);
			}
		}else {
			chain.doFilter(request, response);
		}
	}
	@Override
	public void init(FilterConfig config) throws ServletException {
		this.casServerLoginUrl = config.getInitParameter("casServerLoginUrl");
	}
}
